package module.building.view.buildingUpPanel
{
	import code.utils.TimeUtil;
	
	import com.darwin.darwinUI.assist.IGroupItem;
	import com.darwin.darwinUI.containers.Box;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.TabPanelBase;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.core.UISprite;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.utils.Dictionary;
	
	import module.building.model.dataObj.BuildingObj;
	import module.building.view.panelUtils.IconLabel;
	import module.main.model.dataObj.UserInfoObj;
	import module.main.view.ResourceConst;

	/**
	 * 建筑升级选项卡面板
	 * @author killsoon
	 */	
	public class BuildingUpTabPanel extends TabPanelBase
	{
		private var imageBg:UIComponent;
		private var starPanel:UIComponent;
		private var buildingImage:UIComponent;
		private var _starNum:int;
		private var lbdescription:Label;
		private var lbExplain:Label;
		private var lbBaseSource:Label;
		private var lbSpecialSource:Label;
		private var lbwood:Label;
		private var lbiron:Label;
		private var lbgold:Label;
		private var lbstone:Label;
		private var labelNeedTime:Label;
		public var canUp:Boolean = true;
		private var _userInfo:UserInfoObj;
		private var _data:BuildingObj;
		
		private var _dataDic:Dictionary;
		
		private const GREEN:uint = 0xb5fd53;
		private const RED:uint = 0xff3300;
		
		public function set userInfo(value:UserInfoObj):void
		{
			_userInfo = value;
		}
		
		public function get userInfo():UserInfoObj
		{
			return _userInfo;
		}
		
		public function set dataDic(value:Dictionary):void
		{
			_dataDic = value;	
		}
		
		public function get dataDic():Dictionary
		{
			return _dataDic;
		}
		
		override public function tabSelectionChange($tab:IGroupItem):void
		{
			canUp = true;
			var object:Object = dataDic[$tab];
//			lbgold.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [userInfo.gold, object.gold]);
			lbgold.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [object.gold]);
			if(userInfo.gold >= object.gold)
			{
				lbgold.textColor = GREEN;
			}else {
				canUp = false;
				lbgold.textColor = RED;
			}
//			lbwood.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [userInfo.wood, object.wood]);
			lbwood.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [object.wood]);
			if(userInfo.wood >= object.wood)
			{
				lbwood.textColor = GREEN;
			}else {
				canUp = false;
				lbwood.textColor = RED;
			}
//			lbiron.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [userInfo.iron, object.iron]);
			lbiron.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [object.iron]);
			if(userInfo.iron >= object.iron)
			{
				lbiron.textColor = GREEN;
			}else {
				canUp = false;
				lbiron.textColor = RED;
			}
//			lbstone.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [userInfo.stone, object.stone]);
			lbstone.text = UIManager.getLanguageManager().getText("buildingLevelUp_resource_enough", [object.stone]);
			if(userInfo.stone >= object.stone)
			{
				lbstone.textColor = GREEN;
			}else {
				canUp = false;
				lbstone.textColor = RED;
			}
		}
		
		public function BuildingUpTabPanel()
		{
			super();
			initView();
		}
		
		
		
		public function set data(buildingObj:BuildingObj):void
		{
			_data = buildingObj;
			setBuildingImage();
			var nextLevel:int = _data.level + 1;
			if(nextLevel > 6){nextLevel = 6;}
			starNum(nextLevel);
			descript(_data.description);
//			trace(_data.description);
			explain(nextLevel, _data.name);
			labelNeedTime.text = TimeUtil.minutesToData(_data.levelInfo[nextLevel-1].time).toString();
			trace(labelNeedTime.text);
		}
		
		private function initView():void
		{
			var bg:UIComponent = new UIComponent();
			bg.backgroundSkin = ResourceConst.BUILD_IMAGE_CONTENT_BG;
			bg.backgroundAuto = true;
			bg.background.height = 228;
			addChild(bg);
			
			imageBg = new UIComponent();
			imageBg.backgroundSkin = ResourceConst.BUILD_WINDOWS_BG;
			imageBg.backgroundAuto = true;
			imageBg.x = 10;
			imageBg.y = 10;
			addChild(imageBg);
			
			buildingImage = new UIComponent();
			imageBg.addChild(buildingImage);
			buildingImage.x = 10;
			buildingImage.y = 40;
			
			var rect:UISprite = new UISprite();
			rect.graphics.beginFill(0x000000);
			rect.graphics.drawRect(0, 0, 165, 22);
			rect.graphics.endFill();
			rect.alpha = 0.6;
			rect.x = 6;
			rect.y = 148;
			imageBg.addChild(rect);
			
			lbExplain = TextFieldFactory.getInstance().getLabel("buildingLevelUp_level");
			rect.addChild(lbExplain);
			
			var sourceBg:UIComponent = new UIComponent();
			sourceBg.backgroundSkin = ResourceConst.SOURCE_BG;
			sourceBg.backgroundAuto = true;
			sourceBg.x = 193;
			sourceBg.y = 10;
			sourceBg.background.height = 70;
			addChild(sourceBg);
						
			lbBaseSource = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_normal");
			lbBaseSource.x = 10;
			lbBaseSource.y = 4;
			sourceBg.addChild(lbBaseSource);
			
			var sourceContainer:Box = new Box();
			sourceContainer.width = sourceBg.background.width;
			sourceContainer.itemDistance = 10;
			lbstone = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
			var stone:IconLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_STONE_ICON,"",lbstone);
			
			lbiron = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
			var iron:IconLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_IRON_ICON,"",lbiron);
			
			lbwood = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
			var wood:IconLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_WOOD_ICON,"",lbwood);
			
			lbgold = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
			var gold:IconLabel = new IconLabel(ResourceConst.MAIN_KEY+ResourceConst.RESOURCEINFO_GOLD_ICON,"",lbgold);
			
			sourceContainer.addItem(gold);
			sourceContainer.addItem(stone);
			sourceContainer.addItem(wood);
			sourceContainer.addItem(iron);
			
			sourceContainer.x = 10;
			sourceContainer.y = 24;
			sourceBg.addChild(sourceContainer);
			
			var specialSourceBg:UIComponent = new UIComponent();
			specialSourceBg.backgroundSkin = ResourceConst.SOURCE_BG;
			specialSourceBg.backgroundAuto = true;
			specialSourceBg.x = 193;
			specialSourceBg.y = 82;
			specialSourceBg.background.height = 106;
			addChild(specialSourceBg);
			
			var lbSpecialSource:Label = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_special");
			lbSpecialSource.x = 10;
			lbSpecialSource.y = 4;
			specialSourceBg.addChild(lbSpecialSource);
			
			var textNeedTime:Label = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_special");
			textNeedTime.text = "升级所需时间：";
			textNeedTime.x = 10;
			textNeedTime.y = 190;
			bg.addChild(textNeedTime);
			
			labelNeedTime = TextFieldFactory.getInstance().getLabel("building_tips_commercialBuildingOrder_time");
			labelNeedTime.x = textNeedTime.x + textNeedTime.textWidth;
			labelNeedTime.y = 190;
			bg.addChild(labelNeedTime);
//			stone.backgroundSkin = ResourceConst.BUILD_ICON_STONE;
//			stone.backgroundAuto = true;
//			stone.background.width = 10;
//			stone.background.height = 10;
//			stone.width = stone.background.width * 1.2;
//			stone.height = stone.background.height * 1.2;
//			sourceBg.addChild(stone);
//			stone.x = lbBaseSource.x - 3;
//			stone.y = lbBaseSource.y + 25;
			
//			lbstone.x = stone.x - 5;
//			lbstone.y = stone.y + 23;
//			sourceBg.addChild(lbstone);
			
			
//			wood = new UIComponent();
//			wood.backgroundSkin = ResourceConst.BUILD_ICON_WOOD;
//			wood.width = wood.background.width * 1.2;
//			wood.height = wood.background.height * 1.2;
//			sourceBg.addChild(wood);
//			wood.x = stone.x + 70;
//			wood.y = stone.y;
//			
//			lbwood = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
//			lbwood.x = wood.x - 5;
//			lbwood.y = wood.y + 23;
//			sourceBg.addChild(lbwood);
//			
//			gold = new UIComponent();
//			gold.backgroundSkin = ResourceConst.BUILD_ICON_GOLD;
//			gold.width = gold.background.width * 0.8;
//			gold.height = gold.background.height * 0.8;
//			sourceBg.addChild(gold);
//			gold.x = wood.x + 70;
//			gold.y = wood.y;
//			
//			lbgold = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
//			lbgold.x = gold.x - 5;
//			lbgold.y = gold.y + 23;
//			sourceBg.addChild(lbgold);
//			
//			iron = new UIComponent();
//			iron.backgroundSkin = ResourceConst.BUILD_ICON_IRON;
//			iron.width = iron.background.width * 1.2;
//			iron.height = iron.background.height * 1.2;
//			sourceBg.addChild(iron);
//			iron.x = gold.x + 70;
//			iron.y = wood.y;
//			
//			lbiron = TextFieldFactory.getInstance().getLabel("buildingLevelUp_resource_enough");
//			lbiron.x = iron.x - 15;
//			lbiron.y = iron.y + 23;
//			sourceBg.addChild(lbiron);
			
			var descritionBg:UIComponent = new UIComponent();
			descritionBg.backgroundSkin = ResourceConst.SOURCE_BG;
			descritionBg.backgroundAuto = true;
			descritionBg.background.width = bg.background.width - 20;
			descritionBg.background.height = 58;
			descritionBg.x = 10;
			descritionBg.y = 231;
			addChild(descritionBg);
			
			lbdescription = new Label();//TextFieldFactory.getInstance().getLabel("");
			descritionBg.addChild(lbdescription);
		}
		
		private function descript(string:String):void
		{
			lbdescription = TextFieldFactory.getInstance().getLabel("buildingLevelUp_description", [UIManager.getLanguageManager().getText(string)]);
		}
		
		private function setBuildingImage():void
		{
			var tempData:BuildingObj = _data.clone();
			tempData.level += 1;
			if(tempData.level > 6){tempData.level = 6;}
			
			buildingImage.addChild(tempData.getIcon(160));
		}
		
		private function explain(level:int, name:String):void
		{
			lbExplain.text = UIManager.getLanguageManager().getText("buildingLevelUp_level", [level, UIManager.getLanguageManager().getText(name)]);
			lbExplain.x = (165 - (lbExplain.width)) / 2;
		}
		
		private function starNum(num:int):void
		{
			for(var i:int = 0; i < num; i++)
			{
				var starYes:UIComponent = new UIComponent();
				starYes.backgroundSkin = ResourceConst.LEVEL_STAR_YES;
				starYes.backgroundAuto = true;
				starYes.x = 9 + 18 * i;
				starYes.y = 11;
				imageBg.addChild(starYes);
			}
			
			for(var j:int = i; j < 6; j++)
			{
				var starNo:UIComponent = new UIComponent();
				starNo.backgroundSkin = ResourceConst.LEVEL_STAR_NO;
				starNo.backgroundAuto = true;
				starNo.x = 9 + 18 * j;
				starNo.y = 11;
				imageBg.addChild(starNo);
			}
		}
	}
}